// @Author miaoqing
// @Date 2024/02/26 10:00:00
// @Desc
package logic

import (
	"core/common"
	"core/component/function"
	"core/component/logger"
	"sync"
)

var (
	cs   *CenterServer
	onCs sync.Once
)

type CenterServer struct {
	gatewayServer  *sync.Map
	pvpServer      *sync.Map
	tcpServer      *sync.Map
	onlineHttpPort *sync.Map
}

func centerServerInstance() *CenterServer {
	onCs.Do(func() {
		cs = &CenterServer{
			gatewayServer:  &sync.Map{},
			pvpServer:      &sync.Map{},
			tcpServer:      &sync.Map{},
			onlineHttpPort: &sync.Map{},
		}
	})
	return cs
}

func (cs *CenterServer) addGatewayServer(serverID, addr string) {
	if _, ok := cs.gatewayServer.Load(serverID); ok {
		return
	}
	srv := &common.ServerInfo{
		ServerID: serverID,
		Addr:     addr,
	}
	cs.gatewayServer.Store(serverID, srv)
	logger.Infof("添加gateway服务 serverID: %v ADDR: %v", serverID, addr)
}

func (cs *CenterServer) removeGatewayServer(serverID string) {
	cs.gatewayServer.Delete(serverID)
	logger.Infof("移除gateway服务 serverID:%v", serverID)
}

func (cs *CenterServer) getAllGatewayServerInfo() []*common.ServerInfo {
	sInfos := make([]*common.ServerInfo, 0)
	cs.gatewayServer.Range(func(key, value any) bool {
		gs, ok := value.(*common.ServerInfo)
		if !ok {
			return true
		}
		sInfos = append(sInfos, gs)
		return true
	})
	return sInfos
}

func (cs *CenterServer) addPvpServer(serverID, addr string) {
	if _, ok := cs.pvpServer.Load(serverID); ok {
		return
	}
	srv := &common.ServerInfo{
		ServerID: serverID,
		Addr:     addr,
	}
	cs.pvpServer.Store(serverID, srv)
	logger.Infof("添加pvp服务 serverID: %v ADDR: %v", serverID, addr)
}

func (cs *CenterServer) removePvpServer(serverID string) {
	cs.pvpServer.Delete(serverID)
	logger.Infof("移除pvp服务 serverID:%v", serverID)
}

func (cs *CenterServer) getAllPvpServerInfo() []*common.ServerInfo {
	sInfos := make([]*common.ServerInfo, 0)
	cs.pvpServer.Range(func(key, value any) bool {
		gs, ok := value.(*common.ServerInfo)
		if !ok {
			return true
		}
		sInfos = append(sInfos, gs)
		return true
	})
	return sInfos
}

func (cs *CenterServer) addTcpServer(serverID, addr string, port int) {
	if _, ok := cs.tcpServer.Load(serverID); ok {
		return
	}
	srv := &common.ServerInfo{
		ServerID: serverID,
		Addr:     addr,
		Port:     port,
	}
	cs.tcpServer.Store(serverID, srv)
	logger.Infof("添加Tcp服务 serverID: %v ADDR: %v", serverID, addr)
}

func (cs *CenterServer) removeTcpServer(serverID string) {
	cs.tcpServer.Delete(serverID)
	logger.Infof("移除Tcp服务 serverID:%v", serverID)
}

func (cs *CenterServer) getAllTcpServerInfo() []*common.ServerInfo {
	sInfos := make([]*common.ServerInfo, 0)
	cs.tcpServer.Range(func(key, value any) bool {
		gs, ok := value.(*common.ServerInfo)
		if !ok {
			return true
		}
		sInfos = append(sInfos, gs)
		return true
	})
	return sInfos
}

func (cs *CenterServer) addOnlinePort(srvID string, httpPort int) {
	if _, ok := cs.onlineHttpPort.Load(srvID); ok {
		return
	}
	cs.onlineHttpPort.Store(srvID, httpPort)
	logger.Infof("添加OnlineHttp srvID:%v Port:%v", srvID, httpPort)
}

func (cs *CenterServer) removeOnlinePort(srvID string, httpPort int) {
	cs.onlineHttpPort.Delete(srvID)
	logger.Infof("移除OnlineHttp  srvID:%v Port:%v", srvID, httpPort)
}

func (cs *CenterServer) getARandServerInfo() *common.ServerInfo {
	val := function.RandSyncMap(cs.tcpServer)
	if val == nil {
		return nil
	}
	value, ok := cs.tcpServer.Load(val)
	if !ok {
		return nil
	}
	return value.(*common.ServerInfo)
}
